package com.aat.dizena.assist.web.controller;


/**
 * @time 2016-05-25
 * @author 新歌<xingeking@163.com>
 * @email xingeking@163.com
 * @version v1.0
 * @function 生成图片验证码
 * @info java spring aop
 * @update none
 */
public class MakeImage {

	private static final String[] chars = { "0", "1", "2", "3", "4", "5", "6",
			"7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "L", "K",
			"M", "N", "P", "R", "S", "T", "V", "W", "Y", "Z", "X" };
	private static final int SIZE = 4;
	/** 字母的个数 */
	private static final int LINES = 6;
	/** 干扰线条数量 */
	private static final int WIDTH = 90;
	/** 图片的宽度 */
	private static final int HEIGHT = 35;
	/** 图片的高度 */
	private static final int FONT_SIZE = 22;

	/** 字体大小及Y坐标 */

	// 验证码
	public static java.util.HashMap<String, java.awt.image.BufferedImage> birthCode() {
		java.util.Random rand = new java.util.Random();
		String key = "";
		java.util.HashMap<String, java.awt.image.BufferedImage> map = new java.util.HashMap<String, java.awt.image.BufferedImage>();
		map.clear();
		java.awt.image.BufferedImage image = new java.awt.image.BufferedImage(
				WIDTH, HEIGHT, java.awt.image.BufferedImage.TYPE_INT_RGB);
		// 获得画笔,画出矩形背景
		java.awt.Graphics g = image.getGraphics();
		g.setColor(java.awt.Color.LIGHT_GRAY);
		g.fillRect(0, 0, WIDTH, HEIGHT);
		// 获得小点的颜色,画出很多点点
		for (int i = 0; i < 228; i++) {
			g.setColor(getPointColor(180, 255));
			int x = rand.nextInt(WIDTH);
			int y = rand.nextInt(HEIGHT);
			int xl = rand.nextInt(12);
			int yl = rand.nextInt(12);
			g.fillOval(x, y, 2, 2);
			g.drawLine(x, y, x + xl, y + yl);
		}
		// 画上字符串
		for (int i = 1; i <= SIZE; i++) {
			int r = rand.nextInt(chars.length);
			key += chars[r];
			g.setColor(new java.awt.Color(20 + rand.nextInt(110), 20 + rand
					.nextInt(110), 20 + rand.nextInt(110)));
			g.setFont(new java.awt.Font("", java.awt.Font.BOLD
					+ java.awt.Font.ITALIC, FONT_SIZE));
			g.drawString(chars[r], (i - 1) * (WIDTH - 5) / SIZE + 5, FONT_SIZE);

		}
		// 画出一些线段干扰
		for (int i = 1; i <= LINES; i++) {
			g.setColor(getLineColor());
			g.drawLine(rand.nextInt(WIDTH / 3), rand.nextInt(HEIGHT / 2),
					rand.nextInt(WIDTH), rand.nextInt(HEIGHT));
		}
		// //////////////////////////
		map.put(key, image);
		return map;
	}

	// 生成0~266范围颜色
	private static java.awt.Color getLineColor() {
		java.util.Random ran = new java.util.Random();
		java.awt.Color color = new java.awt.Color(ran.nextInt(256),
				ran.nextInt(256), ran.nextInt(256));
		return color;
	}

	// 生成在一定数值范围内的颜色MIN~max
	private static java.awt.Color getPointColor(int min, int max) {
		java.util.Random random = new java.util.Random();
		if (min > 255)
			min = 255;
		if (max > 255)
			max = 255;
		int r = min + random.nextInt(max - min);
		int g = min + random.nextInt(max - min);
		int b = min + random.nextInt(max - min);
		return new java.awt.Color(r, g, b);
	}

}